#!/bin/bash 
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

path=/nginx/wp
path1=/nginx/she
authusers=rsyncuser
uid=www
passwordfile=/etc/rsync.pass
. /etc/os-release


color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}






config_rsync(){
if [[ $ID =~ centos|rocky  ]];then
	cat > /etc/rsyncd.conf << EOF
uid = $uid
gid = $uid
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = $authusers
secrets file = $passwordfile
log file = /var/log/rsyncd.log
[backup]
comment = welcome to oldboyedu backup!
path = $path




EOF
	if [[ ! `id 666;echo $?` -eq 0  ]];then
		groupadd -g666 www
		useradd -u666 -g666 -M -s /sbin/nologin www
	fi
		
	if [[  -f $passwordfile || !  -f $passwordfile ]];then
		 echo "${authusers}:123456" > $passwordfile
	         chmod 600 $passwordfile
		echo -e "\E[1;32m 用户：rsyncuser 密码：123456 \E[0m"
	fi	
	[[ -d $path ]] || mkdir -p $path;chown -R ${uid}.${uid} $path
	[[ -d $path1 ]] || mkdir -p $path1;chown -R ${uid}.${uid} $path1

fi
}
start_file(){
	systemctl start rsyncd	
	systemctl enable rsyncd
	systemctl is-active rsyncd
	if [[ $? -eq 0 ]];then
		color "rsync服务配置成功" 0
	else 
		color "rsync服务配置失败" 1
		exit 1
	fi
		

}
config_rsync
start_file

